﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Manager_MainMenu : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
           Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        if (!IsPostBack)
        {
            BindMenu();
            lblLoginName.Text = Session["UserName"].ToString();
            // tvwmenu.Attributes.Add("onclick", "fnTopPage()");
        }
    }

    private void BindMenu()
    {
        string URLRoot = ResolveUrl("~/");
        TreeNode tnInfo = new TreeNode();
        tnInfo.Text = "Home Page";
        tnInfo.ToolTip = tnInfo.Text;
        tnInfo.ImageUrl = @"~\Images\Icon\Homepage.jpg";
        tnInfo.NavigateUrl = "~/Default.aspx";
        tnInfo.Target = "fParent";
        tvwmenu.Nodes.Add(tnInfo);

       /* tnInfo = new TreeNode();   
        tnInfo.ImageUrl = @"~\Images\Icon\HomeAdmin.jpg";
        tnInfo.Text = GetURL("0", "Home Admin", "Manager/SummaryHome.aspx");
        tnInfo.ToolTip = tnInfo.Text;
        tvwmenu.Nodes.Add(tnInfo);*/
        TreeNode childInfo;
        if (Session["IsAdmin"].ToString() != "S")
        {
            tnInfo = new TreeNode();
            tnInfo.Text = "Thông tin cá nhân";
            tnInfo.ToolTip = tnInfo.Text;
            tnInfo.ImageUrl = @"~\Images\Icon\user.png";
            tnInfo.Collapse();
            tnInfo.SelectAction = TreeNodeSelectAction.Expand;
            tvwmenu.Nodes.Add(tnInfo);
            

            childInfo = new TreeNode();
            childInfo.ToolTip = childInfo.Text;
            childInfo.Text = GetURL("0", "Thông tin của bạn", "Manager/PrivateInfo/PrivateInfo.aspx");
            childInfo.ToolTip = tnInfo.Text;
            childInfo.ImageUrl = @"~\Images\Icon\Dummy_user.png";
            tnInfo.ChildNodes.Add(childInfo);

            childInfo = new TreeNode();

            childInfo.Text = GetURL("0", "Thay đổi Email", "Manager/PrivateInfo/ChangeEmail.aspx");
            childInfo.ToolTip = childInfo.Text;
            childInfo.Target = "ifBody";
            childInfo.ImageUrl = @"~\Images\Icon\Dummy_user.png";
            tnInfo.ChildNodes.Add(childInfo);

            childInfo = new TreeNode();

            childInfo.Text = GetURL("0", "Thay đổi mật khẩu",  "Manager/PrivateInfo/ChangePassword.aspx");
            childInfo.ToolTip = childInfo.Text;
            childInfo.Target = "ifBody";
            childInfo.ImageUrl = @"~\Images\Icon\Dummy_user.png";
            tnInfo.ChildNodes.Add(childInfo);
        }
        clsWebShop cls = new clsWebShop();
        DataTable dt = null;
        dt = cls.Get_MenuNoneProc().Tables[0];

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow myRow in dt.Rows)
            {
                if ((myRow["LevelMenuID"].ToString() == "0"))
                {
                    TreeNode tnChild = CreateItem(myRow, "N");
                    if (tnChild != null)
                    {
                        tvwmenu.Nodes.Add(tnChild);
                        PopulateSubMenu(dt, myRow["MenuID"].ToString(), tnChild);
                    }
                }
            }
        }

        if (Session["IsAdmin"].ToString() == "S")
        {

            tnInfo = new TreeNode();
            tnInfo.Text = "Quản lý menu";  
            tnInfo.ToolTip = tnInfo.Text;
            tnInfo.ImageUrl = @"~\Images\Icon\mncon.png";
            // tnInfo.Target = "ifBody";
            tvwmenu.Nodes.Add(tnInfo);

            childInfo = new TreeNode();   
            childInfo.Text = GetURL("-1", "Cập nhật menu",  "Manager/Menu/Menu.aspx");
            childInfo.ToolTip = childInfo.Text;     
            childInfo.ImageUrl = @"~\Images\Icon\mnchild.png";
            childInfo.Target = "ifBody";
            tnInfo.ChildNodes.Add(childInfo);

            /* childInfo = new TreeNode();
             childInfo.Text = " Quyền hạn menu";
             childInfo.NavigateUrl = "~/Manager/Menu/MenuD.aspx?index=1&CodeID=-1";
             childInfo.ImageUrl = @"~\Images\Icon\mnchild.png";
             tnInfo.ChildNodes.Add(childInfo);*/

            childInfo = new TreeNode();
            childInfo.Text = GetURL("-1", "Người dùng menu",  "Manager/Menu/MenuD.aspx?index=0");
            childInfo.ToolTip = childInfo.Text;
            childInfo.NavigateUrl = "~/Manager/Menu/MenuD.aspx?index=0&CodeID=-1";
            childInfo.ImageUrl = @"~\Images\Icon\mnchild.png";
            childInfo.Target = "ifBody";
            tnInfo.ChildNodes.Add(childInfo);

            childInfo = new TreeNode(); 
            childInfo.Text = GetURL("-1", "Giải mã",  "Manager/PrivateInfo/show.aspx?index=0");
            childInfo.ToolTip = childInfo.Text;
            childInfo.NavigateUrl = "~/";
            childInfo.ImageUrl = @"~\Images\Icon\mnchild.png";
            childInfo.Target = "ifBody";
            tnInfo.ChildNodes.Add(childInfo);
        }

    }

    private TreeNode CreateItem(DataRow myRow, string leaf_mk)
    {
        bool bcheckItem = false;
        if (Session["IsAdmin"].ToString() == "S")
        {
            bcheckItem = true;
        }
        else
        {
            clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), myRow["MenuID"].ToString());
            bcheckItem = clsAuth.IsShowAuth();
        }

        if (bcheckItem)
        {
            TreeNode tnChild = new TreeNode();
            if (leaf_mk == "N")
                tnChild.ImageUrl = @"~\Images\Icon\IconAGroup.png";
            else
                tnChild.ImageUrl = @"~\Images\Icon\iconAchild.png";

            tnChild.ToolTip = tnChild.Text;
            if (myRow["MenuURL"].ToString() != "")
            {
                tnChild.Text = GetURL(myRow["MenuID"].ToString(), myRow["MenuName"].ToString(), myRow["MenuURL"].ToString());
            }
            else
            {
                tnChild.Text = myRow["MenuName"].ToString();
                tnChild.SelectAction = TreeNodeSelectAction.Expand;
            }
            return tnChild;
        }
        return null;

    }

    private void PopulateSubMenu(DataTable myApMenuData, string menu_id, TreeNode tnChild)
    {

        foreach (DataRow childRow in myApMenuData.Rows)
        {

            if (childRow["LevelMenuID"].ToString() == menu_id)
            {
                string leaf_mk = "Y";
                TreeNode childItem = CreateItem(childRow, leaf_mk);
                if (childItem != null)
                {
                    tnChild.ChildNodes.Add(childItem);
                    PopulateSubMenu(myApMenuData, childRow["MenuID"].ToString(), childItem);
                }


            }
        }
    }

    private string GetURL(string ID, string Name,string URL)
    {
        if (URL.IndexOf("?") == -1)
            URL = URL + "?CodeID=" + ID;
        else
            URL = URL + "&CodeID=" + ID;
        return "<a href='javascript:void(0)'  class='TextMenuRoot'  onclick=\"openURL('" + Name + "','" + ResolveUrl("~/") + URL + "') \">" + Name + "</a>";

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.RemoveAll();
        Session.Abandon();
        Session.Clear();
        // Response.Redirect("~/Manager/default.aspx");
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>window.open('Default.aspx','_top');</script>");
            return;
        }
    }
}
